##
# This class handles the user-groups
class AdminGroupController < ApplicationController
	helper :right
	include RightHelper
  layout  'scaffold'
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

	# session must be active!
	verify :session => 'user',
				 :add_flash => { :error => 
					 							 'you must be logged in to manage user accounts' },
				 :redirect_to => { :controller => 'user', :action => :login}

  # verifies that the currently logged in user has the appropriate rights to
  # access this controller
	before_filter :verify_admin


  ##
  # lists all groups
  def list
    @group_pages, @groups = paginate :groups, :per_page => 10
  end

  ##
  # shows one group
  def show
    @group = Group.find(params[:id])
  end

  ##
  # creates a creation-form
  def new
    @group = Group.new
  end

  ##
  # stores a new group in the DB
  def create
    @group = Group.new(params[:group])
    if @group.save
      flash[:notice] = 'Group was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  ##
  # creats an edit-form
  def edit
    @group = Group.find(params[:id])
  end

  ##
  # updates one group in the DB
  def update
    @group = Group.find(params[:id])
    if @group.update_attributes(params[:group])
      flash[:notice] = 'Group was successfully updated.'
      redirect_to :action => 'show', :id => @group
    else
      render :action => 'edit'
    end
  end

  ##
  # deletes one group from the DB
  def destroy
    Group.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
